/**
 * Created by zhangpeng on 2017/6/5.
 */

/**
 *右侧div显示
 */
function hideDiv() {
    hideShowTableTd('contentTable', '8,9,10,11', '2');
    $("#LeftDiv").attr("class", "main-content col-lg-8");
    $("#RightDiv").show('normal');
}

/**
 * 右侧div隐藏
 */
function showDiv() {
    hideShowTableTd('contentTable', '1,2,3,4,5,6，7,8,9,10,11,12,13', '1');
    $("#RightDiv").attr("style", "display: none");
    $("#LeftDiv").attr("class", "main-content col-lg-12");
    $("#LeftDiv").show('normal');
}


/**
 *添加主表
 * @param ApplyId
 * @returns {*}
 */
function addClinic(showDiv, url) {
    hideDiv();
    return loadDiv(showDiv, url);
}

///**
// * 关闭主表添加div
// */
//$(function () {
//    $("#btnCancel").on('click', function () {
//        showDiv();
//    })
//})

///**
// * 关闭子表添加div
// */
//function hideSubDiv(){
//    showDiv();
//}

/**
 *打开子表div
 * @param id
 * @param tr
 */
function addSubProjectDiv(showDiv, url) {
    loadDiv(showDiv, url);
    $("#itemTableList").treeTable({expandLevel: 2});
}

/**
 *添加子表信息
 */
function addSubProject(showDiv, applyId, url) {
    if (applyId != "" && applyId != null) {
        hideDiv();
        return loadDiv(showDiv, url);
    } else {
        toastr.info("请选择需要的治疗模板后再添加项目！");
    }

}

function addSubOrder(type,showDiv, url){
    itemTable
    var subId = "";
    var performFreq = "";
    var administration = "";
    //subId = $("#itemTable .table-selected").find("td").eq(0).text();
    var subId = $("#itemTable .table-selected").attr("id");
    if(subId != "" && subId != null){
        hideDiv();
        $("#"+showDiv).load(url+'&parent.id='+subId,'',function(response,status,xhr){
            $("select").select2({matcher: function (term, text) {
                term = term.toUpperCase();
                if (makePy(text)[0].indexOf(term) != -1 || text.indexOf(term) != -1) {
                    return text;
                }
                return "";
            }
            });
            if(type == 1){
                performFreq = $("#itemTable .table-selected").find("td").eq(3).find("input").val();
                administration = $("#itemTable .table-selected").find("td").eq(4).find("input").val();
            } else {
                performFreq = $("#itemTable .table-selected").find("td").eq(4).find("input").val();
                administration = $("#itemTable .table-selected").find("td").eq(5).find("input").val();
            }

            $("#performFreq option[value='"+performFreq+"']").attr("selected","selected");
            $("#performFreq").change();
            $("#administration option[value='"+administration+"']").attr("selected","selected");
            $("#administration").change();

        });
        //return loadDiv(showDiv, );
    }else {
        toastr.info("请先选择一条主医嘱！");
    }
}


/**
 * 保存子表
 * @param id
 * @returns {*}
 */
function saveSub(divId, formId, url, hrefUrl,applyId) {
    //formSaveLoad(divId, fromId, divUrl, fromUrl);
    //function formSaveLoad(divId, formId, url, hrefUrl) {
    if (applyId != "" && applyId != null) {
        if ($('#' + formId).parsley().validate()) {
            setTimeout(formSaveMethod(divId, formId, url, hrefUrl), 300);
        }
        return false;
    } else {
        toastr.info("请选择需要的治疗模板后再添加项目！");
    }

    }
    function formSaveMethod(divId, formId, url, hrefUrl) {
        url=timestamp(url);
        hrefUrl=timestamp(hrefUrl);
        if (flag) {
            flag = false;
            formSubmitInput(formId);
            $("#" + formId + " input,#" + formId + " select").removeAttr("disabled");
            $("#" + formId).ajaxSubmit({
                type: 'post', // 提交方式 get/post
                url:url, // 需要提交的 url
                success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
                    if (data.code == "success") {
                        toastr.success(data.data);
                        loadDiv(divId, hrefUrl);
                        showDiv();
                    } else if(data.code == 'info'){
                        loadDiv(divId, hrefUrl);
                    } else {
                        flag = true;
                        toastr.error(data.data);
                    }
                }
            });
        }
        return false;

}


/*中西药局切换*/
function deptChange2(inputId) {
    var value = $.trim($("#" + inputId).val());
    var text = $("#" + inputId + " option[value=" + value + "]").text();
    if (text == '西药局' || text == '西药房') {
        $("input:radio[value='A']").prop("checked", true);
    } else if (text == '中药局' || text == '中药房') {
        $("input:radio[value='B']").prop("checked", true);
    }
}



/**
 * 弹出层 加载父级div
 * @param div
 * @param divUrl
 * @param url
 */
function parentDivLoad(div,formId, divUrl, url) {

    $("#" + formId).ajaxSubmit({
        type: 'post', // 提交方式 get/post
        url:url, // 需要提交的 url
        success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
            if (data.code == "success") {
                parent.toastr.success(data.data);
                parent.loadDiv(div, divUrl);
                parent.layer.closeAll();
            } else if(data.code == 'info'){
                parent.layer.closeAll();
                parent.loadDiv(div, divUrl);
                parent.layer.closeAll();
            } else {
                flag = true;
                parent.toastr.error(data.data);
            }
        }
    });

}


function parentDivLoadItem(div,divItem,formId, divUrl, url,urlItem) {

    $("#" + formId).ajaxSubmit({
        type: 'post', // 提交方式 get/post
        //data:data,
        url:url, // 需要提交的 url
        success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
            if (data.code == "success") {
                parent.toastr.success(data.data);
                parent.loadDiv(div, divUrl);
                parent.loadDiv(divItem, urlItem);
                parent.layer.closeAll();
            } else if(data.code == 'info'){
                parent.layer.closeAll();
                parent.loadDiv(div, divUrl);
                parent.layer.closeAll();
            } else {
                flag = true;
                parent.toastr.error(data.data);
            }
        }
    });

}




